<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>opencv切割图片 | 博客</title><meta name="keywords" content="python"><meta name="author" content="小魂"><meta name="copyright" content="小魂"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一: 使用环境本教材是windows系统下，基于python3的opencv 配置python环境，反手丢一个安装教程：https:&#x2F;&#x2F;www.liaoxuefeng.com&#x2F;wiki&#x2F;1016959663602400&#x2F;1016959856222624  python装好的时候就可以去配置opencv了，反手在丢一个opencv的安装教程：https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_">
<meta property="og:type" content="article">
<meta property="og:title" content="opencv切割图片">
<meta property="og:url" content="https://shark-yun.gitee.io/blog/2019/12/17/opencv/opencv_split_img/index.html">
<meta property="og:site_name" content="博客">
<meta property="og:description" content="一: 使用环境本教材是windows系统下，基于python3的opencv 配置python环境，反手丢一个安装教程：https:&#x2F;&#x2F;www.liaoxuefeng.com&#x2F;wiki&#x2F;1016959663602400&#x2F;1016959856222624  python装好的时候就可以去配置opencv了，反手在丢一个opencv的安装教程：https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://shark-yun.gitee.io/blog/img/opencv/opencv_split_img/opencv_split_img_style.jpg">
<meta property="article:published_time" content="2019-12-17T14:27:06.000Z">
<meta property="article:modified_time" content="2021-02-27T10:04:18.974Z">
<meta property="article:author" content="小魂">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://shark-yun.gitee.io/blog/img/opencv/opencv_split_img/opencv_split_img_style.jpg"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://shark-yun.gitee.io/blog/2019/12/17/opencv/opencv_split_img/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-02-27 18:04:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"> <script src="/blog/live2d-widget/autoload.js"></script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/blog/img/style/touxiang.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">26</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">14</div></a></div></div></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/blog/img/opencv/opencv_split_img/opencv_split_img_style.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">博客</a></span><div id="menus"><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">opencv切割图片</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-12-17T14:27:06.000Z" title="发表于 2019-12-17 22:27:06">2019-12-17</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-02-27T10:04:18.974Z" title="更新于 2021-02-27 18:04:18">2021-02-27</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="一-使用环境"><a href="#一-使用环境" class="headerlink" title="一: 使用环境"></a>一: 使用环境</h1><p>本教材是windows系统下，基于python3的opencv</p>
<p>配置python环境，反手丢一个安装教程：<a target="_blank" rel="noopener" href="https://www.liaoxuefeng.com/wiki/1016959663602400/1016959856222624">https://www.liaoxuefeng.com/wiki/1016959663602400/1016959856222624</a> </p>
<p>python装好的时候就可以去配置opencv了，反手在丢一个opencv的安装教程：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_35684521/article/details/81953047">https://blog.csdn.net/weixin_35684521/article/details/81953047</a></p>
<p>当然，此教材还用到了一些库<br><br><br>在cmd中输入pip install numpy安装numpy库</p>
<h1 id="二-opencv简介"><a href="#二-opencv简介" class="headerlink" title="二: opencv简介"></a>二: opencv简介</h1><p>首先，先介绍一下OpenCV，openCV是一个跨平台的库，使用它我们可以开发实时的计算机视觉应用程序。要集中在图像处理，视频采集和分析，包括人脸检测和物体检测等功能。</p>
<p>想要了解更多可以去看百度百科：<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/opencv/10320623?fr=aladdin">https://baike.baidu.com/item/opencv/10320623?fr=aladdin</a></p>
<h1 id="三：opencv处理并切割图片"><a href="#三：opencv处理并切割图片" class="headerlink" title="三：opencv处理并切割图片"></a>三：opencv处理并切割图片</h1><p>当你安装完opencv的时候，我们就可以愉快的用opencv玩耍了，首先打开我们的 <code>pycharm</code>创建项目，点击工具栏  File -&gt; New project 在location里选择好路径并给工程一个名称img_split如下图：</p>
<p><img src="creat_project.png" alt="工程名称"></p>
<h2 id="创建工程"><a href="#创建工程" class="headerlink" title="创建工程"></a>创建工程</h2><p>创建好工程以后我们创建一个 <code>img</code> 存放切割图片的文件夹和 <code>split.py</code> 文件如下图:</p>
<p><img src="filename_and_py.png" alt="创建文件夹和程序"></p>
<h2 id="读取图片"><a href="#读取图片" class="headerlink" title="读取图片"></a>读取图片</h2><p>创建完把 1.jpg 图片放到img文件夹，在<code>split.py</code>文件下<br></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import cv2  # 先把我们的opencv库给导进来</span><br><span class="line">import numpy as np # 把numpy库给导进来</span><br><span class="line"></span><br><span class="line"># 把图片用opencv读入</span><br><span class="line">img &#x3D; cv2.imread(&quot;img&#x2F;1.jpg&quot;) </span><br><span class="line"></span><br><span class="line"># 显示图片</span><br><span class="line">cv2.namedWindow(&quot;img&quot;, 2)</span><br><span class="line">cv2.imshow(&quot;img&quot;, img)  </span><br><span class="line">cv2.waitKey(0)</span><br><span class="line">cv2.destroyAllWindows()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>这样你就能看到img下的1.jpg了。</p>
<h2 id="阈值图片"><a href="#阈值图片" class="headerlink" title="阈值图片"></a>阈值图片</h2><p>当然这不是最有趣的，我们现在用opencv对这张图片进行阈值处理。<br>简单阈值最为简单，就是选取一个全局阈值，然后把整幅图像分成非黑即白的二值图像。代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">import cv2</span><br><span class="line">import numpy as np</span><br><span class="line"></span><br><span class="line"># 读入图片</span><br><span class="line">img &#x3D; cv2.imread(&quot;img&#x2F;1.jpg&quot;)</span><br><span class="line">print(img[:20])</span><br><span class="line"></span><br><span class="line"># 灰度化图片</span><br><span class="line">img_gry &#x3D; cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)</span><br><span class="line">print(img_gry[:20])</span><br><span class="line"></span><br><span class="line"># 阈值图片</span><br><span class="line">ret, img_th &#x3D; cv2.threshold(img_gry, 100, 255, cv2.THRESH_BINARY_INV)</span><br><span class="line">print(img_th[:20])</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">cv2.namedWindow(&quot;img_th&quot;, 2)</span><br><span class="line">cv2.imshow(&quot;img_th&quot;, img_th)</span><br><span class="line">cv2.waitKey(0)</span><br><span class="line">cv2.destroyAllWindows()</span><br></pre></td></tr></table></figure>
<p>处理完的图片如图：<br><br><img src="Cut_pictures.png" alt="阈值"></p>
<p>在阈值之前我们先对图片灰度化，使用opencv读取图片的时候，默认使用的是BGR来读取图片的，原始读取的图片是3通道的，经过灰度转换之后变成了单通道。</p>
<p>阈值的方法有很多，在这里我们选择用 <code>cv2.THRESH_BINARY_INV</code>用这个参数来处理我们的图片，方便我们以后对图片的处理。可以从图中明显的看到，文字变成了白色，而其他地方都变成了黑色，形成了只有黑色和白色的图片，这就是阈值所说的把整幅图像分成非黑即白的二值图像的意思。</p>
<p>我们就可以用numpy的方法去处理图片，实现我们对图片的切割定位。<br>通过上面代码运行的控制台的输出信息，我们可以清楚的看到，opencv读进来是三维数组，灰度化以后将一个像素点的<code>[B，G，R]</code>信息给转化成了一个数，所以三维数组转化成了二维数组。<br>然后我们用numpy矩阵的mean方法，把二维数组压缩成一维数组，然后用numpy的的一些方法去处理现在的数组。现在为了方便调试代码，我们把显示图片的代码注释调，现在代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">import cv2</span><br><span class="line">import numpy as np</span><br><span class="line"></span><br><span class="line"># 读入图片</span><br><span class="line">img &#x3D; cv2.imread(&quot;img&#x2F;1.jpg&quot;)</span><br><span class="line"></span><br><span class="line"># 灰度化图片</span><br><span class="line">img_gry &#x3D; cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)</span><br><span class="line"></span><br><span class="line"># 阈值图片</span><br><span class="line">ret, img_th &#x3D; cv2.threshold(img_gry, 100, 255, cv2.THRESH_BINARY_INV)</span><br><span class="line">img_th_mean &#x3D; np.mean(img_th, axis&#x3D;1)</span><br><span class="line">img_th_mean_index &#x3D; np.where(img_th_mean &lt; 10)[0]</span><br><span class="line">print(img_th_mean_index)</span><br><span class="line">print(img_th_mean[img_th_mean_index])</span><br><span class="line"></span><br><span class="line"># cv2.namedWindow(&quot;img_th&quot;, 2)</span><br><span class="line"># cv2.imshow(&quot;img_th&quot;, img_th)</span><br><span class="line"># cv2.waitKey(0)</span><br><span class="line"># cv2.destroyAllWindows()</span><br></pre></td></tr></table></figure>
<p>第12行代码，我们把阈值后的图片的像素信息压缩y轴。</p>
<p>第13行代码，我们取出数组值小于10点的索引信息。</p>
<p>第14行和第15行代码是分别是输出小于10的索引信息的点的索引信息，输出数组中小于10的值。<br>我们就得到了所有像素小于10的索引信息。<br>为了方便读者观察，我把压缩y轴的像素信息用plt工具包画出来如图：</p>
<p><img src="coordinate_curve.png" alt="像素信息"></p>
<p>从上面的图像我们就可以很容易的看出像素的信息。</p>
<h2 id="切割图片"><a href="#切割图片" class="headerlink" title="切割图片"></a>切割图片</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 阈值选择出来的数据，进行下标错位相减</span><br><span class="line">img_th_mean_index_diff &#x3D; img_th_mean_index[1:]-img_th_mean_index[:-1]</span><br><span class="line"></span><br><span class="line"># 寻找跨度非常大的点</span><br><span class="line">img_th_mean_index_diff_index &#x3D; np.where(img_th_mean_index_diff&gt;10)[0]</span><br><span class="line"></span><br><span class="line"># 上下切割点</span><br><span class="line">up &#x3D; img_th_mean_index[img_th_mean_index_diff_index]</span><br><span class="line">down &#x3D; img_th_mean_index[img_th_mean_index_diff_index + 1]</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>上面这个代码可以说是我们这个切图算法的精髓所在之处了，我们先利用数组进行错位相减，得到坐标错位相减的索引信息，然后利用numpy的where函数找的跨度非常大的数据的索引信息，然后在上切割点<code>img_th_mean_index[img_th_mean_index_diff_index]</code>下切割点<code>img_th_mean_index[img_th_mean_index_diff_index + 1]</code>。<br>这样我们就得到了两组数据分别储存着所有的上切割点，和所有的下切割点，切up和down一一对应,这时候我们可以随便拿出来一组数据来看看我们切割的图片是否能出来：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">img_split_one &#x3D; img[up[1]:down[1]]</span><br><span class="line"></span><br><span class="line">cv2.namedWindow(&quot;img_split_one&quot;, 2)</span><br><span class="line">cv2.imshow(&quot;img_split_one&quot;, img_split_one)</span><br><span class="line">cv2.waitKey(0)</span><br><span class="line">cv2.destroyAllWindows()</span><br></pre></td></tr></table></figure>

<p><img src="img_split_one.png" alt="切割图片"></p>
<p>很明显第二行切割是正确的，那么我们写一个for循环就能把所有的行图片切割并保存了，下面是算法的完整代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">import cv2</span><br><span class="line">import numpy as np</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 读入图片</span><br><span class="line">img &#x3D; cv2.imread(&quot;img&#x2F;1.jpg&quot;)</span><br><span class="line"></span><br><span class="line"># 灰度化图片</span><br><span class="line">img_gry &#x3D; cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)</span><br><span class="line"></span><br><span class="line"># 阈值图片</span><br><span class="line">ret, img_th &#x3D; cv2.threshold(img_gry, 100, 255, cv2.THRESH_BINARY_INV)</span><br><span class="line">img_th_mean &#x3D; np.mean(img_th, axis&#x3D;1)</span><br><span class="line">img_th_mean_index &#x3D; np.where(img_th_mean &lt; 10)[0]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 阈值选择出来的数据，进行下标错位相减</span><br><span class="line">img_th_mean_index_diff &#x3D; img_th_mean_index[1:]-img_th_mean_index[:-1]</span><br><span class="line"></span><br><span class="line"># 切割点在y_index_diff里面的位置，也是y_index的右切割点位置下标</span><br><span class="line">img_th_mean_index_diff_index &#x3D; np.where(img_th_mean_index_diff&gt;10)[0]</span><br><span class="line"></span><br><span class="line"># 左右切割点</span><br><span class="line">up &#x3D; img_th_mean_index[img_th_mean_index_diff_index]</span><br><span class="line">down &#x3D; img_th_mean_index[img_th_mean_index_diff_index + 1]</span><br><span class="line"></span><br><span class="line">if len(up) &#x3D;&#x3D; len(down):</span><br><span class="line">    for split_num in range(len(up)):</span><br><span class="line">        img_split &#x3D; img[up[split_num]:down[split_num]]</span><br><span class="line">        save_name &#x3D; &quot;img&#x2F;split&quot; + str(split_num) + &quot;.jpg&quot;</span><br><span class="line">        print(save_name)</span><br><span class="line">        cv2.imwrite(save_name, img_split)</span><br></pre></td></tr></table></figure>
<p>下面是切割完的图片</p>
<p><img src="img_spilt_all.png" alt="切割完的图片"></p>
<p>切割完的图片旋转一下继续用上面的方法切割，就可以得到单个文字的图片，在这读者可以自己去尝试一下。<br>本项目的切割图片<br><img src="1.jpg" alt="待切割图片"></p>
<h2 id=""><a href="#" class="headerlink" title=""></a><img src="one.gif" alt="动画"></h2><p>如果本文对你有巨大的帮助的话，可以在下面打赏一包辣条哦！不甚感激💗</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">小魂</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://shark-yun.gitee.io/blog/2019/12/17/opencv/opencv_split_img/">https://shark-yun.gitee.io/blog/2019/12/17/opencv/opencv_split_img/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://shark-yun.gitee.io/blog" target="_blank">博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/python/">python</a></div><div class="post_share"><div class="social-share" data-image="/blog/img/opencv/opencv_split_img/opencv_split_img_style.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/blog/" target="_blank"><img class="post-qr-code-img" src="/blog/"/></a><div class="post-qr-code-desc"></div></li><li class="reward-item"><a href="/blog/" target="_blank"><img class="post-qr-code-img" src="/blog/"/></a><div class="post-qr-code-desc"></div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2019/12/30/%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%90%E7%BB%B4/nginx%E5%AE%89%E8%A3%85/"><img class="prev-cover" src="/blog/img/%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%90%E7%BB%B4/nginx%E5%AE%89%E8%A3%85/theme.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">nginx安装</div></div></a></div><div class="next-post pull-right"><a href="/blog/2019/03/17/django%E6%95%99%E7%A8%8B/django%E7%AC%AC%E4%B8%80%E8%8A%82/"><img class="next-cover" src="/blog/img/%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%90%E7%BB%B4/nginx%E5%AE%89%E8%A3%85/theme.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">django</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2020/06/09/如何优雅的写出python代码/如何优雅的写出python代码/" title="如何优雅的写出python代码"><img class="cover" src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-09</div><div class="title">如何优雅的写出python代码</div></div></a></div><div><a href="/blog/2020/06/09/git教程/git常用命令大全/如何优雅的写出python代码/" title="如何优雅的写出python代码"><img class="cover" src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-09</div><div class="title">如何优雅的写出python代码</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/blog/img/style/touxiang.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">小魂</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">26</div></a></div><div class="card-info-data-item is-center"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">14</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/shangxiaohun/dashboard/projects" target="_blank" title="Github"><i class="fa fa-github"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80-%E4%BD%BF%E7%94%A8%E7%8E%AF%E5%A2%83"><span class="toc-number">1.</span> <span class="toc-text">一: 使用环境</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C-opencv%E7%AE%80%E4%BB%8B"><span class="toc-number">2.</span> <span class="toc-text">二: opencv简介</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%EF%BC%9Aopencv%E5%A4%84%E7%90%86%E5%B9%B6%E5%88%87%E5%89%B2%E5%9B%BE%E7%89%87"><span class="toc-number">3.</span> <span class="toc-text">三：opencv处理并切割图片</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%B7%A5%E7%A8%8B"><span class="toc-number">3.1.</span> <span class="toc-text">创建工程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AF%BB%E5%8F%96%E5%9B%BE%E7%89%87"><span class="toc-number">3.2.</span> <span class="toc-text">读取图片</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%98%88%E5%80%BC%E5%9B%BE%E7%89%87"><span class="toc-number">3.3.</span> <span class="toc-text">阈值图片</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%87%E5%89%B2%E5%9B%BE%E7%89%87"><span class="toc-number">3.4.</span> <span class="toc-text">切割图片</span></a></li><li class="toc-item toc-level-2"><a class="toc-link"><span class="toc-number">3.5.</span> <span class="toc-text"></span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/04/21/%E6%98%BE%E7%A4%BA%E5%99%A8/xianshiqi/" title="Linux开启显示器内置声卡"><img src="/blog/img/%E6%98%BE%E7%A4%BA%E5%99%A8/picture/index.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Linux开启显示器内置声卡"/></a><div class="content"><a class="title" href="/blog/2021/04/21/%E6%98%BE%E7%A4%BA%E5%99%A8/xianshiqi/" title="Linux开启显示器内置声卡">Linux开启显示器内置声卡</a><time datetime="2021-04-21T05:08:50.000Z" title="发表于 2021-04-21 13:08:50">2021-04-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/02/09/%E6%95%99%E7%A8%8B/%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87/" title="环境准备"><img src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="环境准备"/></a><div class="content"><a class="title" href="/blog/2021/02/09/%E6%95%99%E7%A8%8B/%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87/" title="环境准备">环境准备</a><time datetime="2021-02-09T02:59:50.000Z" title="发表于 2021-02-09 10:59:50">2021-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/02/07/%E6%95%99%E7%A8%8B/%E5%88%86%E6%9E%90%E5%BB%BA%E5%BA%93%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C/" title="分析建库准备工作"><img src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="分析建库准备工作"/></a><div class="content"><a class="title" href="/blog/2021/02/07/%E6%95%99%E7%A8%8B/%E5%88%86%E6%9E%90%E5%BB%BA%E5%BA%93%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C/" title="分析建库准备工作">分析建库准备工作</a><time datetime="2021-02-07T08:15:50.000Z" title="发表于 2021-02-07 16:15:50">2021-02-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/09/27/%E9%80%81%E7%BB%99%E5%B0%8F%E5%AD%A6%E5%BC%9F%E7%9A%84%E4%B8%80%E5%B0%81%E4%BF%A1/%E9%80%81%E7%BB%99%E5%B0%8F%E5%AD%A6%E5%BC%9F%E7%9A%84%E4%B8%80%E5%B0%81%E4%BF%A1/" title="送给小学弟的一封信"><img src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="送给小学弟的一封信"/></a><div class="content"><a class="title" href="/blog/2020/09/27/%E9%80%81%E7%BB%99%E5%B0%8F%E5%AD%A6%E5%BC%9F%E7%9A%84%E4%B8%80%E5%B0%81%E4%BF%A1/%E9%80%81%E7%BB%99%E5%B0%8F%E5%AD%A6%E5%BC%9F%E7%9A%84%E4%B8%80%E5%B0%81%E4%BF%A1/" title="送给小学弟的一封信">送给小学弟的一封信</a><time datetime="2020-09-26T18:20:50.000Z" title="发表于 2020-09-27 02:20:50">2020-09-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/07/11/%E5%82%85%E7%AB%8B%E5%8F%B6%E5%8F%98%E6%8D%A2/%E5%82%85%E7%AB%8B%E5%8F%B6%E5%8F%98%E6%8D%A2/" title="傅立叶变换"><img src="/blog/img/opencv/opencv_split_img/opencv_split_img_style.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="傅立叶变换"/></a><div class="content"><a class="title" href="/blog/2020/07/11/%E5%82%85%E7%AB%8B%E5%8F%B6%E5%8F%98%E6%8D%A2/%E5%82%85%E7%AB%8B%E5%8F%B6%E5%8F%98%E6%8D%A2/" title="傅立叶变换">傅立叶变换</a><time datetime="2020-07-11T15:53:50.000Z" title="发表于 2020-07-11 23:53:50">2020-07-11</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 小魂</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">你只管努力，剩下的交给时间</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>